package com.pactera.miyuangroup.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pactera.miyuangroup.db.entity.LoginRole;
import com.pactera.miyuangroup.db.entity.LoginUser;
import com.pactera.miyuangroup.db.mapper.LoginRoleMapper;
import com.pactera.miyuangroup.service.LoginRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 登录角色关系表 服务实现类
 * </p>
 *
 * @author WangWu
 * @since 2018-04-13
 */
@Service
public class LoginRoleServiceImp extends ServiceImpl<LoginRoleMapper, LoginRole> implements LoginRoleService {

    /**
     * 更新用户角色权限
     *
     * @param userId  用户ID
     * @param rolesId 角色集合
     * @return
     */
    @Override
    @Transactional
    public boolean updateUserRoles(String userId, List<String> rolesId) {
        //先解除所有关系
        EntityWrapper<LoginRole> ew = new EntityWrapper<LoginRole>();
        ew.eq("login_user_id", userId);
        boolean flg = delete(ew);
        //新增角色
        List<LoginRole> roles = new ArrayList<>();
        LoginRole role = null;
        for (String rloeId : rolesId) {
            role = new LoginRole();
            role.setLoginUserId(userId);
            role.setRoleId(rloeId);
            roles.add(role);
        }
        flg = insertBatch(roles);
        return flg;
    }
}
